/**
 二次axios函数

 @params option 弹出参数表，
 {
        url : 同axios,
        data : get/post参数,
        header : 同axios,
        method : 同axios,
    }
 */
// import Qs from 'qs'
// let baseURL = 'http://192.168.0.114:8090';
let baseURL='/'

function ajax(option) {
    return axios({
        baseURL: baseURL,
        timeout: 10000,
        headers: option.headers ? option.headers : {'content-type': 'application/x-www-form-urlencoded'},
        url: option.url,
        method: option.method ? option.method : 'get',
        params: (option.method == 'get' || option.method == 'GET') ? option.data : '',
        data: (option.method == 'post' || option.method == 'POST') ? Qs.stringify(option.data) : ''
    })
        .then(res => {
            console.log("werew11", res)
            return new Promise(function (resolve,reject) {
                if (res.data.code == 0) {
                    resolve(res);
                } else if (res.data.code == 401) {
                    var url = window.location.href;
                    window.location.href = `/api/login/mp/auth?url=${url}`;
                } else if (res.data.code == 404) {
                    var url = window.location.href;
                    window.location.href = `/api/login/mp/auth`;
                } else {
                    reject(res.data);
                    // console.log(error,"======");
                }
            })
        });
}

export default ajax;